/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Linq;
using System.Collections.Generic;

namespace moose.irc {

	/// <summary>
	/// Manages Addresses
	/// </summary>
	[CLSCompliant(true)]
	public class IrcUsersList
	{
		
		#region [Local Attributes]
		
		/// <summary>
		/// Collection of Users
		/// </summary>
		private List<IrcUser> m_users;
		
		#endregion
		
		#region [Construction]
		
		/// <summary>
		/// Initializes IRC Address List 
		/// </summary>
		public IrcUsersList() : base()
		{
			m_users = new List<IrcUser>();
			return;
		}
		
		#endregion
		
		#region [Public Methods]
		
		/// <summary>
		/// Get or create and get an IRC User 
		/// </summary>
		/// <param name="user_string">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="IrcUser"/>
		/// </returns>
		public IrcUser get(string user_string) {

            var users = from u in m_users
                        where u.Name == user_string
                        select u;
            if (users.Count() > 0)
            {
                return users.First();
            }
            else
            {
                IrcUser user = new IrcUser(user_string);
                m_users.Add(user);
                return user;
            }
                
		}

        /// <summary>
        /// Get or add IRC user, by reference
        /// </summary>
        /// <param name="user">user object reference</param>
        /// <returns>the same object handed in, but will append it to the collection if it isnt already there</returns>
        public IrcUser get(IrcUser user)
        {
            if (!m_users.Contains(user))
                m_users.Add(user);
            

            return user;

        }

        /// <summary>
        /// Remove user from collection
        /// </summary>
        /// <param name="user_string">user string representing the user</param>
        public void remove(string user_string)
        {
            var users = from u in m_users
                        where u.Name == user_string
                        select u;
            if (users.Count() > 0)
            {
                m_users.Remove(users.First());
            }
            return;
        }

        /// <summary>
        /// Remove User from Colleciton
        /// </summary>
        /// <param name="user">reference to user to remove</param>
        public void remove(IrcUser user)
        {
            if (!m_users.Contains(user))
                m_users.Remove(user);
            return;
        }


		#endregion
		
	}
}
